package com.glowdayz.glowmee.bc;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.glowdayz.common.DBHandler;
import com.glowdayz.common.PageHandler;
import com.glowdayz.glowmee.cmd.ThirdCategoryCMD;
import com.glowdayz.glowmee.dao.SecondCategoryDAO;
import com.glowdayz.glowmee.dao.ThirdCategoryDAO;

public class ThirdCategoryBC extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
	
	public ThirdCategoryBC() {
        super();
    }
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DBHandler.getInstance().connect();
		
		String page_str = request.getParameter("page");
		int idSecond = Integer.parseInt(request.getParameter("idSecond"));
		
		int page = 0;
		if (page_str == null) {
			page = 1;
		} else {
			page = Integer.parseInt(page_str);
		}
		
		String param_str = request.getParameter("param");
		String updateIdSecondCategory_str = request.getParameter("updateIdSecondCategory" + param_str);
		String updateThirdCategoryText = request.getParameter("updateThirdCategoryText" + param_str);
		String updateSortKey_str = request.getParameter("updateSortKey" + param_str);
		String updateIsDisplay_str = request.getParameter("updateIsDisplay" + param_str);
		
		if (param_str != null && updateIdSecondCategory_str != null && updateThirdCategoryText != null && 
				updateSortKey_str != null && updateIsDisplay_str != null) {
			int param = Integer.parseInt(param_str);
			int updateIdSecondCategory = Integer.parseInt(updateIdSecondCategory_str);
			updateThirdCategoryText = new String(updateThirdCategoryText.getBytes("8859_1"), "UTF-8");
			int updateSortKey = Integer.parseInt(updateSortKey_str);
			int updateIsDisplay = Integer.parseInt(updateIsDisplay_str);
			try {
				ThirdCategoryCMD cmd = new ThirdCategoryCMD(param, updateIdSecondCategory, updateThirdCategoryText, updateSortKey, updateIsDisplay);
				ThirdCategoryDAO.getInstance().updateThirdCategory(cmd);
				request.setAttribute("alert", "<script type='text/javascript'>alert('수정이 완료되었습니다.')</script>");
			} catch (Exception e) {
				DBHandler.getInstance().disconnect();
				e.printStackTrace();
			}
		}
		
		int secondTotal = DBHandler.getInstance().getCount("secondcategory", "idSecondCategory", null);
		
		try {
			SecondCategoryDAO second = SecondCategoryDAO.getInstance();
			ThirdCategoryDAO dao = ThirdCategoryDAO.getInstance();
			
			request.setAttribute("page", PageHandler.getInstance());
			request.setAttribute("second", second.getSecondCategoryList(1, secondTotal, null));
			request.setAttribute("secondInfo", second.getSecondCategory(idSecond));
			request.setAttribute("third", dao.getThirdCategoryList(page, 10, "idSecondCategory=" + idSecond));
			request.setAttribute("display", DBHandler.getInstance().getCount(
					"thirdcategory", "idThirdCategory", "isDisplay='1' AND idSecondCategory=" + idSecond));
			request.setAttribute("unDisplay", DBHandler.getInstance().getCount(
					"thirdcategory", "idThirdCategory", "isDisplay='0' AND idSecondCategory=" + idSecond));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		DBHandler.getInstance().disconnect();
		RequestDispatcher dpr = request.getRequestDispatcher("/category/thirdCategory.jsp");
		dpr.forward(request, response);
	}
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		DBHandler.getInstance().connect();
		
		request.setCharacterEncoding("utf-8");
		int insertIdSecondCategory = Integer.parseInt(request.getParameter("insertIdSecondCategory"));
		String insertThirdCategoryText = request.getParameter("insertThirdCategoryText");
		int insertSortKey = Integer.parseInt(request.getParameter("insertSortKey"));
		int insertIsDisplay = Integer.parseInt(request.getParameter("insertIsDisplay"));
		
		try {
			ThirdCategoryCMD cmd = new ThirdCategoryCMD(insertIdSecondCategory, insertThirdCategoryText, insertSortKey, insertIsDisplay);
			ThirdCategoryDAO.getInstance().insertThirdCategory(cmd);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		int secondTotal = DBHandler.getInstance().getCount("secondcategory", "idSecondCategory", null);
		
		try {
			SecondCategoryDAO second = SecondCategoryDAO.getInstance();
			ThirdCategoryDAO dao = ThirdCategoryDAO.getInstance();
			
			request.setAttribute("page", PageHandler.getInstance());
			request.setAttribute("second", second.getSecondCategoryList(1, secondTotal, null));
			request.setAttribute("secondInfo", second.getSecondCategory(insertIdSecondCategory));
			request.setAttribute("third", dao.getThirdCategoryList(1, 10, "idSecondCategory=" + insertIdSecondCategory));
			request.setAttribute("display", DBHandler.getInstance().getCount(
					"thirdcategory", "idThirdCategory", "isDisplay='1' AND idSecondCategory=" + insertIdSecondCategory));
			request.setAttribute("unDisplay", DBHandler.getInstance().getCount(
					"thirdcategory", "idThirdCategory", "isDisplay='0' AND idSecondCategory=" + insertIdSecondCategory));
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		DBHandler.getInstance().disconnect();
		RequestDispatcher dpr = request.getRequestDispatcher("/category/thirdCategory.jsp");
		dpr.forward(request, response);
	}

}
